package com.arch.dev.client.view;

import com.arch.dev.client.Util.MessageObserver;
import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.security.AuthService;
import com.arch.dev.client.security.AuthServiceAsync;
import com.arch.dev.client.view.chart.ExpChartType;
import com.arch.dev.client.view.chart.FlexGraphView;
import com.arch.dev.client.view.chart.GraphView;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class BudgetCalcShell extends Composite {

	private static BudgetCalcShellUiBinder uiBinder = GWT
			.create(BudgetCalcShellUiBinder.class);

	interface BudgetCalcShellUiBinder extends UiBinder<Widget, BudgetCalcShell> {
	}
	
	private final AuthServiceAsync authService = GWT
			.create(AuthService.class);
	
	public BudgetCalcShell() {
		
		leftHandNav = new LeftHandNav();
		messagePanel = new MessagePanel(MessageObserver.getInstance());
		initWidget(uiBinder.createAndBindUi(this));
		
		messagePanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		addBudgetView = new AddBudgetView();
		editBudgetView = new EditBudgetView();
		

		leftHandNav.addViewListener(new ClickHandler() {

			public void onClick(ClickEvent event) {
				contentPanel.clear();
				messagePanel.clear();
				addBudgetView.populateCatagoryListBox();
				contentPanel.add(addBudgetView);
				
				BudgetController.getInstance().getAllValues(addBudgetView);

			}
		});

		leftHandNav.editViewListener(new ClickHandler() {

			public void onClick(ClickEvent event) {
				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(editBudgetView);

			}
		});

		leftHandNav.addView1Listener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(dailyGraphView);
				dailyGraphView.updateGraph();

				BudgetController.getInstance().getDateData(dailyGraphView);
				// BudgetController.getInstance().getMonthlyData(
				// chart1);

			}
		});

		leftHandNav.addView2Listener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				/*
				 * contentPanel.clear(); GChart chart =
				 * budgetCalculatorView.getGraph2(); contentPanel.add(chart);
				 * chart.update();
				 */
				

				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(monthlyGraphView);
				monthlyGraphView.updateGraph();

				BudgetController.getInstance().getDateData(monthlyGraphView);
				

			}
		});

		leftHandNav.addView3Listener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				contentPanel.clear();
				messagePanel.clear();
				//PdfDataPanel pdfDataPanel = new PdfDataPanel();
				final PDFTableView pdfTableView = new PDFTableView();
				
				pdfTableView.addGeneratePDFClickHandler(new ClickHandler() {

					public void onClick(ClickEvent event) {

						BudgetController.getInstance().generatePDF(
								pdfTableView);

					}

				});


				contentPanel.add(pdfTableView);

				BudgetController.getInstance().getAllPDFValues(pdfTableView);

			}
		});
		
		
		leftHandNav.addCatagoryViewListener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(catagoryView);
				BudgetController.getInstance().getAllCatagories(catagoryView);

			}
		});
		
		
		leftHandNav.addUserViewListener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(usersView);
				usersView.refresh();
				//BudgetController.getInstance().getAllCatagories(catagoryView);

			}
		});
		
		flexGraphView = new FlexGraphView("CostExpenseFlex.swf", Window.getClientHeight()-140);
		leftHandNav.addflexGraphViewListener(new ClickHandler() {

			public void onClick(ClickEvent event) {

				contentPanel.clear();
				messagePanel.clear();
				contentPanel.add(flexGraphView);
			
				//BudgetController.getInstance().getAllCatagories(catagoryView);

			}
		});
		
		authService.retrieveUsername(new AsyncCallback<String>() {

		
			public void onFailure(Throwable arg0) {
				// TODO Auto-generated method stub
				
			}

		
			public void onSuccess(String userName) {
				
				userNameLabel.setText("Welcome " + userName + " | ");
				
			}
		});
		
	}

	private AddBudgetView addBudgetView;

	private EditBudgetView editBudgetView;

	private CatagoryView catagoryView = new CatagoryView();
//	private GChart chart1 = new GChartExample01();

	private GraphView dailyGraphView = new GraphView(ExpChartType.DAILY);

	private GraphView monthlyGraphView = new GraphView(ExpChartType.MONTHLY);
	
	private UsersView usersView = new UsersView();
	
	private FlexGraphView flexGraphView;
	
	/**
	 * The panel that holds the content.
	 */
	@UiField
	SimplePanel contentPanel;

	@UiField
	HTML userNameLabel;
	
	
/*	public HTML getUserNameLabel() {
		return userNameLabel;
	}*/

	/**
	 * The container around the links at the top of the app.
	 */
	/*@UiField
	TableElement linkCell;*/

	/**
	 * A drop box used to change the locale.
	 */
	/*@UiField
	ListBox localeBox;
*/
	/**
	 * The container around locale selection.
	 */
	@UiField
	TableCellElement localeSelectionCell;

	/*	  *//**
	 * The main menu used to navigate to examples.
	 */
	/*
	 * @UiField(provided = true) CellTree mainMenu;
	 */

	@UiField(provided = true)
	LeftHandNav leftHandNav;

	@UiField(provided = true)
	HorizontalPanel messagePanel;

	/*  *//**
	 * The button used to show the example.
	 */
	/*
	 * @UiField Anchor tabExample;
	 *//**
	 * The button used to show the source CSS style.
	 */
	/*
	 * @UiField Anchor tabStyle;
	 *//**
	 * The button used to show the source code.
	 */
	/*
	 * @UiField Anchor tabSource;
	 *//**
	 * The list of available source code.
	 */
	/*
	 * @UiField ListBox tabSourceList;
	 */
}
